package com.dh.project.controller;

import com.dh.project.model.EmployeeInfo;
import com.dh.project.model.EngineerPhone;
import com.dh.project.other.ModelRes;
import com.dh.project.service.EmployeeInfoService;
import com.dh.project.service.EngineerPhoneService;
import com.dh.project.utils.DayuSMSUtil;
import com.dh.project.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Date;

/**
 * Created by admin on 2017/4/25.
 */
@Controller
public class EmployeeController {

    private EmployeeInfoService employeeInfoService;

    private EngineerPhoneService engineerPhoneService;

    @Autowired
    public EmployeeController(EmployeeInfoService employeeInfoService, EngineerPhoneService engineerPhoneService) {
        this.employeeInfoService = employeeInfoService;
        this.engineerPhoneService = engineerPhoneService;

    }


    /**
     * 发送验证码
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sendSms", produces = "text/html;charset=utf-8")
    public Object sendSms(String customer_phone) {
        Map map = new HashMap();
        map.put("employee_phone", customer_phone);
        List<EmployeeInfo> employeeInfos = employeeInfoService.selectByMap(map);
        if (employeeInfos.size() == 0) {
            return new ModelRes(ModelRes.Status.ERROR, "没有该用户！");
        } else {
            String mathCode = String.valueOf(nextInt());
            employeeInfos.get(0).setMath_code(mathCode);
            employeeInfoService.updateById(employeeInfos.get(0));
            String json = "{\"code\":\"" + mathCode + "\"}";
//            SmsUtil.sendSms(json, customer_phone, "SMS_89935029");
//            SmsUtil.sendSms(json, customer_phone, "SMS_89935029");
            try {
                DayuSMSUtil.sendSms(json, DayuSMSUtil.WORKERLOGIN, customer_phone);
            } catch (Exception e) {
                e.printStackTrace();
                return new ModelRes(ModelRes.Status.ERROR, "验证码发送异常!");
            }
            return new ModelRes();
        }
    }

    public static int nextInt() {
        int min = 100000;
        int max = 999999;
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (max - min + 1) + min;
    }

    /**
     * 工人登录
     *
     * @param employee_phone
     * @param math_code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "employeeLogin", produces = "text/html;charset=utf-8")
    public Object employeeLogin(String employee_phone, String math_code) {
        Map map = new HashMap();
        map.put("employee_phone", employee_phone);
        List<EmployeeInfo> employeeInfos = employeeInfoService.selectByMap(map);
        if (employeeInfos.size() == 0) {
            return new ModelRes(ModelRes.Status.ERROR, "没有该账号！");
        } else {
            if (!employeeInfos.get(0).getMath_code().equals(math_code)) {
                return new ModelRes(ModelRes.Status.ERROR, "验证码错误，登录失败!");
            }
            employeeInfos.get(0).setModifytime(String.valueOf(new Date().getTime()));
            employeeInfoService.updateById(employeeInfos.get(0));
            return new ModelRes(ModelRes.Status.SUCCESS, "登录成功", employeeInfos.get(0));
        }
    }

    /**
     * 查询工人信息
     *
     * @param employee_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectEmployeeByIds", produces = "text/html;charset=utf-8")
    public Object selectEmployeeById(int employee_id) {
        EmployeeInfo employeeInfo = employeeInfoService.selectById(Long.valueOf(employee_id));
        return new ModelRes(ModelRes.Status.SUCCESS, "查询成功", employeeInfo);
    }

    /**
     * 查询工程师电话列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectEngineerPhoneList", produces = "text/html;charset=utf-8")
    public Object selectEngineerPhoneList() {
        Map map = new HashMap();
        List<EngineerPhone> engineerPhoneList = engineerPhoneService.selectByMap(map);
        return new ModelRes(engineerPhoneList);
    }


}
